package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tanhua.domain.db.PageResult;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mg.Comment;
import com.tanhua.domain.mg.Publish;
import com.tanhua.domain.mg.Video;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PublishDataVo;
import com.tanhua.domain.vo.VideoDataVo;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mg.CommentApi;
import com.tanhua.dubbo.mg.VideoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserDataService {

    //订阅UserInfoApi方法
    @Reference
    private UserInfoApi userInfoApi;
    //订阅PublishApi方法
    @Reference
    private PublishApi publishApi;
    //订阅VideoApi
    @Reference
    private VideoApi videoApi;
    //订阅CommontApi
    @Reference
    private CommentApi commentApi;


    //用户数据翻页
    public PageResult<UserInfo> userDataPage(Integer page, Integer pagesize) {
       return userInfoApi.findUserInfoPage(page,pagesize);
    }
    //用户基本资料
    public UserInfo queryUserData(Integer userID) {
        return  userInfoApi.findUserInfoByID(userID.longValue());
    }

    //动态分页列表展示
    public PageResult<PublishDataVo> publishPageData(Integer pagesize, Integer page, Long uid, Integer state) {
        //c.逻辑层 调用publishAPi传入查询条件 返回pageResult<Publish>对象
        PageResult<Publish> publishPageResult = publishApi.queryPublishByUserId(uid, page, pagesize);
        //d.获得分页对象的items属性
        List<Publish> items = publishPageResult.getItems();
        //e.创建一个存储MomentVo的集合
        List<PublishDataVo> publishDataVos = new ArrayList<>();
        //f.判断items对象是否为null 不为null 遍历
        if(CollectionUtils.isNotEmpty(items)){
            for (Publish publish : items) {
                //g.创建PublishDataVo对象
                PublishDataVo publishDataVo = new PublishDataVo();
                //h.根据publish对象的userId对象 调用userInfoApi方法 返回UserInfo对象
                UserInfo userInfo = userInfoApi.findUserInfoByID(publish.getUserId());
                //i.复制属性到MomentVo对象
                BeanUtils.copyProperties(publish,publishDataVo);
                BeanUtils.copyProperties(userInfo,publishDataVo);
                //j.单独赋值属性
                publishDataVo.setId(publish.getId().toHexString());//发布id
                publishDataVo.setUserId(userInfo.getId().intValue());//发布作者
                if(publish.getMedias()!=null){
                    publishDataVo.setImageContent(publish.getMedias().toArray(new String[]{}));//动态图片
                }
                //创建时间
                publishDataVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(publish.getCreated()));
                publishDataVo.setState(String.valueOf(publish.getState()));//状态信息
                //k.复制pageResult对象属性到pageResult<MomentVo>
                publishDataVos.add(publishDataVo);
            }
        }
        //创建PageResult对象
        PageResult<PublishDataVo> voPageResult=new PageResult<>();
        //复制分页对象属性
        BeanUtils.copyProperties(publishPageResult,voPageResult);
        //单独设置集合属性
        voPageResult.setItems(publishDataVos);
        //返回分页结果
        return voPageResult;
    }
    //动态视频列表展示
    public PageResult<VideoDataVo> videoPageData(Integer pagesize, Integer page, Long uid) {
        int inc=0;
        //c.调用videoApi方法 传入当前用户id 查询分页数据 pageResult<Video>
        PageResult<Video> videoPageResult = videoApi.findVideoByUserId(uid,page,pagesize);
        //d.创建一个集合 存储VideoDataVo
        List<VideoDataVo> videoDataVos = new ArrayList<>();
        //e.判断pageResult<Video>的items属性是否为null 如果不为Null 遍历
        if(CollectionUtils.isNotEmpty(videoPageResult.getItems())){
            for (Video video : videoPageResult.getItems()) {
                //f.创建对象 VideoDataVo
                VideoDataVo videoDataVo = new VideoDataVo();
                //g.根据video的作者id 调用userInfoApi 返回userInfo对象
                UserInfo userInfo = userInfoApi.findUserInfoByID(video.getUserId());
                //h.复制属性
                BeanUtils.copyProperties(userInfo,videoDataVo);
                BeanUtils.copyProperties(video,videoDataVo);
                //i.单独赋值属性
                videoDataVo.setId(++inc);//编号自增
                videoDataVo.setUserId(userInfo.getId().intValue());//用户id
                videoDataVo.setCreateDate(System.currentTimeMillis());//创建时间
                //添加对象到集合中
                videoDataVos.add(videoDataVo);
            }
        }
        //j.赋值pageResult<Video>到PageResult<VideoDataVo>
        PageResult<VideoDataVo> voPageResult=new PageResult<>();
        BeanUtils.copyProperties(videoPageResult,voPageResult);
        //添加集合
        voPageResult.setItems(videoDataVos);
        //返回分页结果
        return voPageResult;
    }

    /**
     * 单条动态详细
     * @param publishId
     * @return
     */
    public PublishDataVo singlePublishData(String publishId) {
        //c.调用publishApi 传入发布id 查询 返回publish
        Publish publish = publishApi.findPublishById(publishId);
        PublishDataVo publishDataVo = new PublishDataVo();
        //判断userId是否为Null
        if(publish.getUserId()!=null){
            //d.根据UserId查询userInfo表 返回UserINfo对象
            UserInfo userInfo = userInfoApi.findUserInfoByID(publish.getUserId());
            //e.复制属性到PublishDataVo
            BeanUtils.copyProperties(publish,publishDataVo);
            BeanUtils.copyProperties(userInfo,publishDataVo);
            //f.单独复制
            publishDataVo.setId(publish.getId().toHexString());//设置发布id
            publishDataVo.setUserId(publish.getUserId().intValue());//作者id
            publishDataVo.setImageContent(publish.getMedias().toArray(new String[]{}));//集合转成数组
            publishDataVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(publish.getCreated()));
            publishDataVo.setState(publish.getState().toString());
        }
        return publishDataVo;
    }
    //评论列表翻页
    public PageResult<CommentVo> commontLinkedData(Integer pagesize, Integer page, String sortProp, String sortOrder, String messageID) {
        //c.调用CommontApi 传入参数 返回pageResult<Commont>对象
       PageResult<Comment> commentPageResult=commentApi.findPageByPublishId(pagesize, page, sortProp, sortOrder, messageID);
        //创建List<CommentVo>对象
        ArrayList<CommentVo> commentVoList = new ArrayList<>();
        //d.判断分页数据的items属性是否为null
        if(CollectionUtils.isNotEmpty(commentPageResult.getItems())){
            for (Comment comment : commentPageResult.getItems()) {
                CommentVo commentVo = new CommentVo();
                //f.根据CommontVo对象的userId查询UserInfo表 返回Userinfo
                UserInfo userInfo = userInfoApi.findUserInfoByID(comment.getUserId());
                //g.复制属性到CommontVo对象
                BeanUtils.copyProperties(comment,commentVo);
                BeanUtils.copyProperties(userInfo,commentVo);
                //h.补全Vo对象 添加到集合中
                commentVo.setId(comment.getId().toHexString());//评论Id
                commentVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(comment.getCreated()));//补全评论时间
                //添加到集合
                commentVoList.add(commentVo);
            }
        }
        //i.创建分页数据 泛型写CommontVo
        PageResult<CommentVo> commentVoPageResult = new PageResult<>();
        //j.复制属性 返回分页数据
        BeanUtils.copyProperties(commentPageResult,commentVoPageResult);
        //设置集合
        commentVoPageResult.setItems(commentVoList);
        //返回集合
        return commentVoPageResult;
    }
}
